﻿Imports LSW.Win32
Imports System.Drawing.Drawing2D
Imports System.Drawing

Namespace Forms
    Public Class MsgBox
        Inherits Form

        Private Shared largeIcon As IntPtr()
        Private Shared smallIcon As IntPtr()

        Private Shared newMessageBox As MsgBox
        Private Shared frmTitle As Label
        Private Shared frmMessage As Label
        Private Shared pIcon As PictureBox
        Private Shared flpButtons As FlowLayoutPanel
        Private Shared frmIcon As Icon

        Private Shared btnOK As Button
        Private Shared btnAbort As Button
        Private Shared btnRetry As Button
        Private Shared btnIgnore As Button
        Private Shared btnCancel As Button
        Private Shared btnYes As Button
        Private Shared btnNo As Button
        Private Shared bt As Button

        Private Shared i As Integer
        Private Shared OldText As String

        Private Shared CYReturnButton As DialogResult

        Public Enum MsgIcon
            [Error]
            Explorer
            Find
            Information
            Mail
            Media
            Print
            Question
            RecycleBinEmpty
            RecycleBinFull
            [Stop]
            User
            Warning
        End Enum

        Public Enum MsgButtons
            Abort
            AbortRetryIgnore
            Cancel
            Ignore
            No
            OK
            OKCancel
            Retry
            RetryCancel
            Yes
            YesNo
            YesNoCancel
        End Enum

        Private Shared Sub BuildMessageBox(title As String)
            newMessageBox = New MsgBox()
            newMessageBox.Text = title
            newMessageBox.Size = New System.Drawing.Size(400, 200)
            newMessageBox.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
            newMessageBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
            AddHandler newMessageBox.Paint, AddressOf newMessageBox_Paint
            newMessageBox.BackColor = System.Drawing.Color.White

            Dim tlp As New TableLayoutPanel()
            tlp.RowCount = 3
            tlp.ColumnCount = 0
            tlp.Dock = System.Windows.Forms.DockStyle.Fill
            tlp.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 22))
            tlp.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100.0F))
            tlp.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 50))
            tlp.BackColor = System.Drawing.Color.Transparent
            tlp.Padding = New Padding(2, 5, 2, 2)

            frmTitle = New Label()
            frmTitle.Dock = System.Windows.Forms.DockStyle.Fill
            frmTitle.BackColor = System.Drawing.Color.Transparent
            frmTitle.ForeColor = System.Drawing.Color.White
            frmTitle.Font = New Font("Tahoma", 9, FontStyle.Bold)

            frmMessage = New Label()
            frmMessage.Dock = System.Windows.Forms.DockStyle.Fill
            frmMessage.BackColor = System.Drawing.Color.White
            frmMessage.Font = New Font("Tahoma", 9, FontStyle.Regular)
            frmMessage.Text = "hiii"

            largeIcon = New IntPtr(249) {}
            smallIcon = New IntPtr(249) {}
            pIcon = New PictureBox()
            ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, 250)

            flpButtons = New FlowLayoutPanel()
            flpButtons.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft
            flpButtons.Padding = New Padding(0, 5, 5, 0)
            flpButtons.Dock = System.Windows.Forms.DockStyle.Fill
            flpButtons.BackColor = System.Drawing.Color.FromArgb(240, 240, 240)

            Dim tlpMessagePanel As New TableLayoutPanel()
            tlpMessagePanel.BackColor = System.Drawing.Color.White
            tlpMessagePanel.Dock = System.Windows.Forms.DockStyle.Fill
            tlpMessagePanel.ColumnCount = 2
            tlpMessagePanel.RowCount = 0
            tlpMessagePanel.Padding = New Padding(4, 5, 4, 4)
            tlpMessagePanel.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50))
            tlpMessagePanel.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100.0F))
            tlpMessagePanel.Controls.Add(pIcon)
            tlpMessagePanel.Controls.Add(frmMessage)

            tlp.Controls.Add(frmTitle)
            tlp.Controls.Add(tlpMessagePanel)
            tlp.Controls.Add(flpButtons)
            newMessageBox.Controls.Add(tlp)
        End Sub

        Private Shared Sub newMessageBox_Paint(sender As Object, e As PaintEventArgs)
            Dim g As Graphics = e.Graphics
            Dim frmTitleL As New Rectangle(0, 0, (newMessageBox.Width / 2), 22)
            Dim frmTitleR As New Rectangle((newMessageBox.Width / 2), 0, (newMessageBox.Width / 2), 22)
            Dim frmMessageBox As New Rectangle(0, 0, (newMessageBox.Width - 1), (newMessageBox.Height - 1))
            Dim frmLGBL As New LinearGradientBrush(frmTitleL, Color.FromArgb(87, 148, 160), Color.FromArgb(209, 230, 243), LinearGradientMode.Horizontal)
            Dim frmLGBR As New LinearGradientBrush(frmTitleR, Color.FromArgb(209, 230, 243), Color.FromArgb(87, 148, 160), LinearGradientMode.Horizontal)
            Dim frmPen As New Pen(Color.FromArgb(63, 119, 143), 1)
            g.FillRectangle(frmLGBL, frmTitleL)
            g.FillRectangle(frmLGBR, frmTitleR)
            g.DrawRectangle(frmPen, frmMessageBox)
        End Sub

        ''' <summary>
        ''' Message: Text to display in the message box.
        ''' </summary>
        Public Overloads Shared Function Show(Message As String) As DialogResult
            BuildMessageBox("")
            frmMessage.Text = Message
            ShowOKButton()
            newMessageBox.ShowDialog()
            Return CYReturnButton
        End Function

        Private Shared Sub ShowOKButton()
            btnOK = New Button()
            btnOK.Text = "OK"
            btnOK.Size = New System.Drawing.Size(80, 25)
            btnOK.BackColor = System.Drawing.Color.FromArgb(255, 255, 255)
            btnOK.Font = New Font("Tahoma", 8, FontStyle.Regular)
            AddHandler btnOK.Click, AddressOf btnOK_Click
            flpButtons.Controls.Add(btnOK)
        End Sub

        Private Shared Sub btnOK_Click(sender As Object, e As EventArgs)
            CYReturnButton = DialogResult.OK
            newMessageBox.Dispose()
        End Sub

        ''' <summary>
        ''' Title: Text to display in the title bar of the messagebox.
        ''' </summary>
        Public Overloads Shared Function Show(Message As String, Title As String) As DialogResult
            BuildMessageBox(Title)
            frmTitle.Text = Title
            frmMessage.Text = Message
            ShowOKButton()
            newMessageBox.ShowDialog()
            Return CYReturnButton
        End Function

        ''' <summary>
        ''' MButtons: Display MyButtons on the message box.
        ''' </summary>
        Public Overloads Shared Function Show(Message As String, Title As String, MButtons As MsgButtons) As DialogResult
            BuildMessageBox(Title)
            ' BuildMessageBox method, responsible for creating the MessageBox
            frmTitle.Text = Title
            ' Set the title of the MessageBox
            frmMessage.Text = Message
            'Set the text of the MessageBox
            ButtonStatements(MButtons)
            ' ButtonStatements method is responsible for showing the appropreiate buttons
            newMessageBox.ShowDialog()
            ' Show the MessageBox as a Dialog.
            Return CYReturnButton
            ' Return the button click as an Enumerator
        End Function

        Private Shared Sub ButtonStatements(MButtons As MsgButtons)
            If MButtons = MsgButtons.AbortRetryIgnore Then
                ShowIgnoreButton()
                ShowRetryButton()
                ShowAbortButton()
            End If

            If MButtons = MsgButtons.OK Then
                ShowOKButton()
            End If

            If MButtons = MsgButtons.OKCancel Then
                ShowCancelButton()
                ShowOKButton()
            End If

            If MButtons = MsgButtons.RetryCancel Then
                ShowCancelButton()
                ShowRetryButton()
            End If

            If MButtons = MsgButtons.YesNo Then
                ShowNoButton()
                ShowYesButton()
            End If

            If MButtons = MsgButtons.YesNoCancel Then
                ShowCancelButton()
                ShowNoButton()
                ShowYesButton()
            End If
        End Sub

        Private Shared Sub ShowAbortButton()
            btnAbort = New Button()
            btnAbort.Text = "Abort"
            btnAbort.Size = New System.Drawing.Size(80, 25)
            btnAbort.BackColor = System.Drawing.Color.FromArgb(255, 255, 255)
            btnAbort.Font = New Font("Tahoma", 8, FontStyle.Regular)
            AddHandler btnAbort.Click, AddressOf btnAbort_Click
            flpButtons.Controls.Add(btnAbort)
        End Sub

        Private Shared Sub ShowRetryButton()
            btnRetry = New Button()
            btnRetry.Text = "Retry"
            btnRetry.Size = New System.Drawing.Size(80, 25)
            btnRetry.BackColor = System.Drawing.Color.FromArgb(255, 255, 255)
            btnRetry.Font = New Font("Tahoma", 8, FontStyle.Regular)
            AddHandler btnRetry.Click, AddressOf btnRetry_Click
            flpButtons.Controls.Add(btnRetry)
        End Sub

        Private Shared Sub ShowIgnoreButton()
            btnIgnore = New Button()
            btnIgnore.Text = "Ignore"
            btnIgnore.Size = New System.Drawing.Size(80, 25)
            btnIgnore.BackColor = System.Drawing.Color.FromArgb(255, 255, 255)
            btnIgnore.Font = New Font("Tahoma", 8, FontStyle.Regular)
            AddHandler btnIgnore.Click, AddressOf btnIgnore_Click
            flpButtons.Controls.Add(btnIgnore)
        End Sub

        Private Shared Sub ShowCancelButton()
            btnCancel = New Button()
            btnCancel.Text = "Cancel"
            btnCancel.Size = New System.Drawing.Size(80, 25)
            btnCancel.BackColor = System.Drawing.Color.FromArgb(255, 255, 255)
            btnCancel.Font = New Font("Tahoma", 8, FontStyle.Regular)
            AddHandler btnCancel.Click, AddressOf btnCancel_Click
            flpButtons.Controls.Add(btnCancel)
        End Sub

        Private Shared Sub ShowYesButton()
            btnYes = New Button()
            btnYes.Text = "Yes"
            btnYes.Size = New System.Drawing.Size(80, 25)
            btnYes.BackColor = System.Drawing.Color.FromArgb(255, 255, 255)
            btnYes.Font = New Font("Tahoma", 8, FontStyle.Regular)
            AddHandler btnYes.Click, AddressOf btnYes_Click
            flpButtons.Controls.Add(btnYes)
        End Sub

        Private Shared Sub ShowNoButton()
            btnNo = New Button()
            btnNo.Text = "No"
            btnNo.Size = New System.Drawing.Size(80, 25)
            btnNo.BackColor = System.Drawing.Color.FromArgb(255, 255, 255)
            btnNo.Font = New Font("Tahoma", 8, FontStyle.Regular)
            AddHandler btnNo.Click, AddressOf btnNo_Click
            flpButtons.Controls.Add(btnNo)
        End Sub

        Private Shared Sub btnAbort_Click(sender As Object, e As EventArgs)
            CYReturnButton = DialogResult.Abort
            newMessageBox.Dispose()
        End Sub

        Private Shared Sub btnRetry_Click(sender As Object, e As EventArgs)
            CYReturnButton = DialogResult.Retry
            newMessageBox.Dispose()
        End Sub

        Private Shared Sub btnIgnore_Click(sender As Object, e As EventArgs)
            CYReturnButton = DialogResult.Ignore
            newMessageBox.Dispose()
        End Sub

        Private Shared Sub btnCancel_Click(sender As Object, e As EventArgs)
            CYReturnButton = DialogResult.Cancel
            newMessageBox.Dispose()
        End Sub

        Private Shared Sub btnYes_Click(sender As Object, e As EventArgs)
            CYReturnButton = DialogResult.Yes
            newMessageBox.Dispose()
        End Sub

        Private Shared Sub btnNo_Click(sender As Object, e As EventArgs)
            CYReturnButton = DialogResult.No
            newMessageBox.Dispose()
        End Sub

        ''' <summary>
        ''' MIcon: Display MsgIcon on the message box.
        ''' </summary>
        Public Overloads Shared Function Show(Message As String, Title As String, MButtons As MsgButtons, MIcon As MsgIcon) As DialogResult
            BuildMessageBox(Title)
            frmTitle.Text = Title
            frmMessage.Text = Message
            ButtonStatements(MButtons)
            IconStatements(MIcon)
            Dim imageIcon As Drawing.Image = New Bitmap(frmIcon.ToBitmap(), 38, 38)
            pIcon.Image = imageIcon
            newMessageBox.ShowDialog()
            Return CYReturnButton
        End Function

        Public Overloads Shared Function Show(Message As String, Title As String, MButtons As MsgButtons, MIcon As MsgIcon, DButtons As MsgButtons, Optional tick As Integer = 30) As DialogResult
            BuildMessageBox(Title)
            frmTitle.Text = Title
            frmMessage.Text = Message
            ButtonStatements(MButtons)
            IconStatements(MIcon)
            Dim imageIcon As Drawing.Image = New Bitmap(frmIcon.ToBitmap(), 38, 38)
            pIcon.Image = imageIcon

            Select Case DButtons
                Case MsgButtons.Abort
                    bt = btnAbort
                Case MsgButtons.Cancel
                    bt = btnCancel
                Case MsgButtons.Ignore
                    bt = btnIgnore
                Case MsgButtons.No
                    bt = btnNo
                Case MsgButtons.Retry
                    bt = btnRetry
                Case MsgButtons.Yes
                    bt = btnYes
                Case Else
                    bt = btnOK
            End Select

            If bt Is Nothing Then Return Show(Message, Title, MButtons, MIcon)

            OldText = bt.Text
            bt.Text = OldText & "(" & tick & ")"

            i = tick - 1

            Dim t As New Timer
            t.Interval = 1000
            t.Enabled = True
            AddHandler t.Tick, AddressOf timer_tick

            newMessageBox.ShowDialog()
            Return CYReturnButton
        End Function

        Private Shared Sub IconStatements(MIcon As MsgIcon)
            If MIcon = MsgIcon.[Error] Then
                MessageBeep(30)
                frmIcon = Icon.FromHandle(largeIcon(109))
            End If

            If MIcon = MsgIcon.Explorer Then
                MessageBeep(0)
                frmIcon = Icon.FromHandle(largeIcon(220))
            End If

            If MIcon = MsgIcon.Find Then
                MessageBeep(0)
                frmIcon = Icon.FromHandle(largeIcon(22))
            End If

            If MIcon = MsgIcon.Information Then
                MessageBeep(0)
                frmIcon = Icon.FromHandle(largeIcon(221))
            End If

            If MIcon = MsgIcon.Mail Then
                MessageBeep(0)
                frmIcon = Icon.FromHandle(largeIcon(156))
            End If

            If MIcon = MsgIcon.Media Then
                MessageBeep(0)
                frmIcon = Icon.FromHandle(largeIcon(116))
            End If

            If MIcon = MsgIcon.Print Then
                MessageBeep(0)
                frmIcon = Icon.FromHandle(largeIcon(136))
            End If

            If MIcon = MsgIcon.Question Then
                MessageBeep(0)
                frmIcon = Icon.FromHandle(largeIcon(23))
            End If

            If MIcon = MsgIcon.RecycleBinEmpty Then
                MessageBeep(0)
                frmIcon = Icon.FromHandle(largeIcon(31))
            End If

            If MIcon = MsgIcon.RecycleBinFull Then
                MessageBeep(0)
                frmIcon = Icon.FromHandle(largeIcon(32))
            End If

            If MIcon = MsgIcon.[Stop] Then
                MessageBeep(0)
                frmIcon = Icon.FromHandle(largeIcon(27))
            End If

            If MIcon = MsgIcon.User Then
                MessageBeep(0)
                frmIcon = Icon.FromHandle(largeIcon(170))
            End If

            If MIcon = MsgIcon.Warning Then
                MessageBeep(30)
                frmIcon = Icon.FromHandle(largeIcon(217))
            End If
        End Sub

        Private Shared Sub timer_tick(sender As Object, e As EventArgs)
            If i > 0 Then
                bt.Text = OldText & "(" & i & ")"
                i -= 1
            Else
                DirectCast(sender, Timer).Enabled = False
                bt.PerformClick()
            End If
        End Sub

    End Class
End Namespace